// File: SplitTool.hpp
//
// Created by 唐建 on 2023/12/1.
//

#ifndef CPP_REMOTE_UBUNTU_SPLITTOOL_HPP
#define CPP_REMOTE_UBUNTU_SPLITTOOL_HPP

#include "simhash/deps/cppjieba/Jieba.hpp"
#include <pthread.h>
#include <vector>
#include <string>

using std::vector;
using std::string;

const char *const DICT_PATH = "../include/simhash/dict/jieba.dict.utf8";
const char *const HMM_PATH = "../include/simhash/dict/hmm_model.utf8";
const char *const USER_DICT_PATH = "../include/simhash/dict/user.dict.utf8";

class SplitToolCppJieba {
private:
    static SplitToolCppJieba *_pSplitToolCppJieba;
    static pthread_once_t _pSplitToolCppJiebaOnce;

    cppjieba::Jieba _Jieba;

public:
    static SplitToolCppJieba *getInstance()
    {
        pthread_once(&_pSplitToolCppJiebaOnce, splitToolInit);
        return _pSplitToolCppJieba;
    }

    vector<string> cut(const string &sentence)
    {
        vector<string> tmp;
        _Jieba.Cut(sentence, tmp, true);
        return tmp;
    }

private:
    SplitToolCppJieba()
    //: _Jieba(DICT_PATH, MODEL_PATH, USER_DICT_PATH, ID_PATH, STOP_WORD_PATH)
            : _Jieba(DICT_PATH, HMM_PATH, USER_DICT_PATH)
    {}

    ~SplitToolCppJieba() = default;

    static void splitToolInit()
    {
        _pSplitToolCppJieba = new SplitToolCppJieba();
        atexit(destroy);
    }

    static void destroy()
    {
        if (_pSplitToolCppJieba) {
            delete _pSplitToolCppJieba;
            _pSplitToolCppJieba = nullptr;
        }
    }
};

#endif //CPP_REMOTE_UBUNTU_SPLITTOOL_HPP
